using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class AuditTrailsRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class AuditTrailsDbo : DatabaseObject
        {
            public AuditTrailsDbo()
            {
                Name = "AuditTrails";
                GeneratedClassName = "AuditTrails";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ErrorType", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "ErrorType" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Source", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Source" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Date", ColumnType = "datetime", IsIdentity = false, IsNullable = false, Length = 8, Precision = 23, Scale = 3, GeneratedPropertyName = "Date" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Comments", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 500, Precision = 0, Scale = 0, GeneratedPropertyName = "Comments" });
              
            }
        }
		
		public static List<AuditTrailRow> Select(Expression<Func<AuditTrailRow, bool>> expression)
        {
            List<AuditTrailRow> rows = new List<AuditTrailRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<AuditTrailRow>(new AuditTrailsDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<AuditTrailRow> Select()
		{
			List<AuditTrailRow> rows = new List<AuditTrailRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<AuditTrailRow>(new AuditTrailsDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<AuditTrailRow> PagedSelect(Expression<Func<AuditTrailRow, bool>> filterExpression, Expression<Func<AuditTrailRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<AuditTrailRow> rows = new List<AuditTrailRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<AuditTrailRow>(new AuditTrailsDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<AuditTrailRow> AuditTrailList)
        {
            AuditTrailRow row;

            while (reader.Read())
            {
                row = new AuditTrailRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					ErrorType = Util.CastDbValue<string>(reader["ErrorType"]),
					Source = Util.CastDbValue<string>(reader["Source"]),
					Date = Util.CastDbValue<DateTime>(reader["Date"]),
					Comments = Util.CastDbValue<string>(reader["Comments"])
                };
                                
                AuditTrailList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(AuditTrailRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<AuditTrailRow>(new AuditTrailsDbo(), row);
            }
        }

		public static void Update(AuditTrailRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<AuditTrailRow>(new AuditTrailsDbo(), row);
            }
        }

		public static void Delete(Expression<Func<AuditTrailRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<AuditTrailRow>(new AuditTrailsDbo(), expression);
            }
        }

	}
}
	
